//
//  HomeCarTypeCell.m
//  CarUser
//
//  Created by 王森林 on 2018/12/4.
//  Copyright © 2018年 王森林. All rights reserved.
//

#import "HomeCarTypeCell.h"
#import "HomeDetailView.h"
#import "ChooseResultViewController.h"
@interface HomeCarTypeCell()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *carAry;
@property (nonatomic, strong) HomeDetailView *homeDetailView;
@end

#define BTNTAG 100000
@implementation HomeCarTypeCell


-(void)createUIWithData
{
    [self.contentView  removeAllSubviews];
    [self.scrollView removeAllSubviews];
    self.scrollView = nil;
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.homeDetailView];
    [self.bgView addSubview:self.scrollView];
    [self createConstraint];
    
    
    float width = (BOUNDWIDTH - 40)/3;
    float height = 90;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(15+(width+10)*(self.carAry.count), self.scrollView.size.height);
    [self.carAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
        NSDictionary *dict  = self.carAry[idx];
      
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame =  CGRectMake(15+(width+10)*idx, 0, width, height);
//        btn.backgroundColor = [UIColor redColor];
        btn.tag = BTNTAG  + idx;
        [btn addTarget:self action:@selector(handleTagerAction:) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, btn.frame.size.width, btn.frame.size.height - 30)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:APPEND_URL(BASE_URL, [dict objectForKey:@"ZST000"])]];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        
        [btn addSubview:imageView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.frame.size.height+5,btn.frame.size.width , 15)];
        titleLabel.text =  [dict objectForKey:@"NAME00"];
        titleLabel.textColor = RGB(51, 51, 51);
        titleLabel.font = FONT_SIZE(14);
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [btn addSubview:titleLabel];
     
        [self.scrollView addSubview:btn];
       
        
    }];
    
}

- (void)handleTagerAction:(id)sender
{
    UIButton  *btn  = sender;
    NSDictionary *dict = self.carAry[btn.tag - BTNTAG];
    
    ChooseResultViewController  *controller  = [[ChooseResultViewController alloc] init];
    controller.type = @"2";
      controller.pushType = 1;
    controller.SERIES_ID = [dict objectForKey:@"SERIES_ID"];
    [[self  viewController].navigationController pushViewController:controller animated:YES];
    
}

- (void)createConstraint
{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(0);
        (void)make.left.and.right;
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
    }];
    
    [self.homeDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(0);
        (void)make.left.and.right;
        make.height.mas_equalTo(48);
    }];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.homeDetailView.mas_bottom).offset(0);
        (void)make.left.and.right;
        make.bottom.mas_equalTo(self.bgView.mas_bottom).offset(0);
    }];
    
}

- (void)setData:(id)data
{
    if (data){
        self.carAry = [NSMutableArray arrayWithArray:data];
        
//        [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            NSDictionary *dict = data[idx];
//            [self.carAry addObject:APPEND_URL(BASE_URL, [dict objectForKey:@"ZST000"])];
//
//        }];
        [self createUIWithData];
    }
}


-  (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
    
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [UIScrollView new];
    }
    return _scrollView;
    
}

- (HomeDetailView *)homeDetailView
{
    if (!_homeDetailView) {
        _homeDetailView = [[HomeDetailView alloc] initWithType:HOME_DETAIL_TYPE_IMPORT withBlock:^(id  _Nonnull response) {
            ChooseResultViewController  *controller  = [[ChooseResultViewController alloc] init];
            controller.type = @"1";
            controller.pushType = 1;
            [[self  viewController].navigationController pushViewController:controller animated:YES];
        }];
    }
    return _homeDetailView;
}

@end
